require(["../../conf/config.js"],function(){
	require(["jquery","ajaxMapping","template","ui","common"],function($,ajaxMapping,template,ui,common){
		
		//判断洋码头的登录状态
		let loginStatus = 0;
		let userObj = null;
		var cookiestr = getCookie("userlist");
		var userArray = JSON.parse(cookiestr);
		(function judge(){
			userArray.forEach(function(item){
				if(item.status == 1){
					loginStatus = 1;
					userObj = item;
					$(".username").html(userObj.username);
					$(".login").css("display","none");
					$(".register").css("display","none");
				}
			})
		})();
		
		console.log(loginStatus)
		//鼠标划入用户名
		var username = document.getElementsByClassName("username")[0];
		$(".username").on("mouseenter",function(){
			var span = document.createElement("span");
			span.innerText = "退出登录"
			span.className = "tuichu";
			username.appendChild(span);
			console.log($(".tuichu"));
			//*****鼠标点击退出登录*********
			span.addEventListener("click",function(){
				console.log("aaa");
				userArray.forEach(function(item){
						item.status = 0;		
				})
				setCookie("userlist", JSON.stringify(userArray), 20);
				loginStatus = 0;
				userObj = null;
				window.location.replace("http://localhost:8000/pages/index/index.html")
			})
		})
		
		//鼠标划出用户名
		$(".username").on("mouseleave",function(){
			var tuichu = document.getElementsByClassName("tuichu")[0];
			tuichu.remove();
		})
		
		//分类菜单渲染
		var classstr = "";
		$.ajax({
			url : ajaxMapping.getCategoryBigList,
			dataType : "jsonp",
			success : function(data){
				var weNeed = data.result;
				$("#proList").load("classify.html",function(){
					
					 classstr = template('classooo',{list:weNeed});
					 console.log(classstr);
					// ????用jquery的html()方法会把script标签也读取出来
					$("#proList").html(classstr);
				})
				
			}
		})
		// 

		var val = 0;
		var index = null;
		//轮播图的左侧切换
		$("#leftBtn").click(()=>{
			val = val+400>0?-3600:val+400;
			index = Math.abs(val/400)+2;
			if(index==11){
				index = 1;
			}
			$("#container>ul").css("left",val + "px");
			$("#main").html(`<img src="../../static/images/lunbo${index}.jpg" >`);
		})
		//轮播图的右侧切换
		$("#rightBtn").click(function(){
			val = val-400<-3600?0:val-400;
			index = Math.abs(val/400)+2;
			if(index==11){
				index = 1;
			}
			$("#container>ul").css("left",val + "px");
			$("#main").html(`<img src="../../static/images/lunbo${index}.jpg" >`);
		})
		
		//在每一个整点设计了一个限时抢的倒计时
		let minBox = document.getElementsByClassName("time")[1];
		let secBox = document.getElementsByClassName("time")[2];
		setInterval(function(){
			var date = new Date();
			var min = 59-date.getMinutes();
			var sec = 59-date.getSeconds();
			if(min>=10){
				minBox.innerHTML = min;
			}else{
				minBox.innerHTML = "0"+min;
			}
			if(sec>=10){
				secBox.innerHTML = sec;
			}else{
				secBox.innerHTML = "0"+sec;
			}
		},1000);
		
		//猜你喜欢部分更新
		var htmlstr = ""
		function guessAjax(){
			$.ajax({
				url : "guessData.json",
				dataType : "json",
				type: "GET",
				success : function(data){
					var weNeed = data;
					console.log(data);
					$("#gussContent").load("guesslist.html",function(){
						
						 htmlstr += template('guess',{list:weNeed});
						// ????用jquery的html()方法会把script标签也读取出来
						document.getElementById("gussContent").innerHTML = htmlstr;
					})
					
				}
			})
		};
		guessAjax();
		
		//鼠标划入点击加载更多
		$("#clickMore").mouseenter(function(){
			$(this).css({"border":"1px solid #cc3333","color":"#cc3333"});
		})
		//鼠标划出加载更多
		$("#clickMore").mouseleave(function(){
			$(this).css({"border":"1px solid #dedede","color":"#383838"});
		})
		//鼠标点击加载更多
		$("#clickMore").click(function(){
			pageNum+=1;
			moreguess();
			
		})
		var pageNum = 1;
		var morestr = "";
		function moreguess(){
			$.ajax({
				url : ajaxMapping.guesslist + "?pageIndex=" + pageNum,
				dataType : "jsonp",
				success : function(data){
					var weNeed = data.result.Products;
					$("#gussContent").load("guesslist.html",function(){
						
						 morestr += template('guess',{list:weNeed});
						document.getElementById("gussContent").innerHTML += morestr;
					})
					
				}
			})
		}
		
		//搜索框的搜索提示
		$("#find").keyup(function(){
			//当敲击键盘是改变输入框内的文本颜色
			$(this).css("color","#000");
			//根据输入框的值获取jsonp数据
			// $.ajax({
			// 	url : ajaxMapping.getPreKeywordList + "?query="+ $("#find").val(),
			// 	dataType : "jsonp",
			// 	success : function(data){
			// 		console.log(data)
			// 		var weFinds = data.result.Keywords;
			// 		$("#find").auto
			// 		
			// 	}
			// })
			$("#find").autocomplete({
				source: function(request, response) {
					$.ajax({
						url: ajaxMapping.getPreKeywordList,
						dataType: "jsonp",
						data: {
							query:request.term
						},
						success: function(data) {
							response(data.result.Keywords);
							console.log(data.result.Keywords);
						}
					});
				},
				minLength: 2,
				//event表示事件对象，ui表示触发此事件的控件。
				select: function(event, ui) {   //*************这个指的输入框
					//item   当条目被选中时  插入到输入框中的值。
					//items  一个数组，数组元素为匹配用户输入的条目。
					console.log("Selected: " + ui.item.value + " aka " + ui.item.id);
					
				}
			});	
		})
		
		//点击回到顶部
		$(".backTop span").click(function(){
			$("html").animate({scrollTop:0},300);
		})
		//鼠标划入二维码
		$(".qrCode").mouseover(function(){
			$(".qrCode span").css({"display":"block"});
		})
		//鼠标划出二维码
		$(".qrCode").mouseleave(function(){
			$(".qrCode span").css("display","none");
		})
		
		
		//进入购物车
		$("#goShopCar").click(function(){
			if(loginStatus==0){
				window.location.replace("http://localhost:8000/pages/login/login.html");
			}else{
				window.location.replace("http://localhost:8000/pages/shopcar/shopcar.html");
			}
		})
		$(".shopCar").click(function(){
			if(loginStatus==0){
				window.location.replace("http://localhost:8000/pages/login/login.html");
			}else{
				window.location.replace("http://localhost:8000/pages/shopcar/shopcar.html");
			}
		})
	})
})